classdef GInterface < glib.GType

  properties (Constant)
    NULL = glib.GType(uint32(1), uint64(0))
  end

  properties
    giInfo = uint64(0)
    name__ = 'NULL'
    meths
  end

  methods
    function varargout = subsref(obj, S, noutrequested)
      if nargin < 3
        noutrequested = nargout;
        indirectReturn = false;
      else
        if nargout > 1, error 'Too many outputs for indirect return', end
        indirectReturn = true;
      end
      if length(S) > 2, error 'Too many subscripts on GInterface', end
      switch S(1).type
        case '.'
          name = S(1).subs;
          if ~obj.meths.isKey(name)
            obj.meths(name) = glib.g_irepository_matlab_find_method(obj.giInfo, name);
          end
          if length(S) == 1
            if noutrequested > 1, error 'Too many outputs for GIFunction', end
            ret = obj.meths(name);
            if indirectReturn, varargout{1} = { ret };
            else varargout{1} = ret; end
          else
            ret = subsref(obj.meths(name), S(2), noutrequested);
            if indirectReturn, varargout{1} = ret;
            else varargout = ret; end
          end % only 1 subscript?
        otherwise
          error 'Unsupported subscript for GInterface'
      end
    end  % function subsref

    %% This is an internal constructor!
    %% To fetch a GInterface, use the repository glib.gi.getRepos()
    function obj = GInterface(gtypetype, gtype, giInfo, name)
      obj@glib.GType(gtypetype, gtype);
      obj.giInfo = giInfo;
      obj.name__ = name;
      obj.meths = containers.Map;
    end

    function delete(obj)
      disp('In GInterface.delete')
      if obj.giInfo
        glib.g_irepository_matlab_release_info(obj.giInfo);
      end
    end

    function disp(obj)
      disp(sprintf('[GInterface %s]', name(obj)));
    end
    
    function s = name(obj)
      s = obj.name__;
    end

  end  % Public Methods

end  % Classdef
